<?php
/**
 * Copyright (c) 2016 PESCMS (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */


namespace App\Home\GET;

class Unit extends Content {

    public function index($display = true) {

        $cid = $this->isG('cid', '请选择您要查看的房屋');

        $result = $this->db('unit')->where('unit_cid = :cid')->select([
            'cid' => $cid,
        ]);

        $list = [];
        if (!empty($result)) {
            foreach ($result as $item) {
                $list[$item['unit_floor']][] = $item;
            }
        }

        $this->assign('list', $list);
        $this->assign('title', '单元管理');
        $this->layout();
    }

    public function fee() {
        $unit = \Model\Content::findContent('unit', $this->g('id'), 'unit_id');
        if (empty($unit)) {
            $this->error('不存在的出租信息');
        }

        $result = $this->db('unit_fee')->where('unit_id = :id')->order('unit_fee_date DESC')->select([
            'id' => $this->g('id'),
        ]);
        $list = [];

        if (!empty($result)) {
            foreach ($result as $item) {
                $sort[$item['unit_fee_date']] = $item['unit_fee_date'];
                $list[$item['unit_fee_date']]['unit_fee_id'] = $item['unit_fee_id'];
                $list[$item['unit_fee_date']][$item['unit_fee_name']] = $item['unit_fee_value'];
                $list[$item['unit_fee_date']]['date'] = date('Y-m', $item['unit_fee_date']);
                $list[$item['unit_fee_date']]['unit_fee_date'] = $item['unit_fee_date'];
            }
            array_multisort($sort, SORT_DESC, $list);
        }

        $this->assign($unit);
        $this->assign('list', $list);
        $this->assign('title', "{$unit['unit_number']}的房租信息");
        $this->layout();
    }

    /**
     * 收据打印
     */
    public function invoice() {

        $name = $this->isG('name', '请提交租房单元名称');
        $fee = $this->isG('fee', '请提交租房的租金');

        $eFee = $this->isG('eFee', '请提交该租房的电费');
        $wFee = $this->isG('wFee', '请提交该租房的水费');

        $eList = explode('-', $this->isG('eList', '请提交该租房的电表明细'));
        $wList = explode('-', $this->isG('wList', '请提交该租房的水表明细'));

        $electric = explode('-', $this->isG('electric', '请提交该租房的用电度数'));
        $water = explode('-', $this->isG('water', '请提交该租房的用水度数'));



        $shouju = PES_CORE . 'Expand/E986LTTN.jpg';
        $image = new \Expand\PHPImage();
        $image->setDimensionsFromImage($shouju);
        $image->draw($shouju);

        $image->setFont(PES_CORE . 'Expand/Font/han.ttf');
        $image->setTextColor([0, 98, 255]);


        $image->text(date('Y    n    ', $this->g('date')),
            [
                'fontSize' => 55,
                'x'        => 1330,
                'y'        => 193,
            ]
        );

        $image->text("{$name} ".date('n', $this->g('date'))."月 租金   ".round($fee)."元.",
            [
                'fontSize' => 50,
                'x'        => 430,
                'y'        => 320,
            ]
        );

        $total = $fee;
		
		$eHeight = 440;
		
		if(count($eList) == 3){
			$wHeight = 660;
		}else{
			$wHeight = 550;
		}
		

        for ($i=0;$i < count($eList);$i++){
            
            if($i == 0){
                $printWidth = 300;
            }else{
                $printWidth += 750;
            }
			
			if($i == 2){
				$printWidth = 300;
				$eHeight += 110;
			}
			
			
			

            $electricFee = empty($electric[$i]) ? 0 : round($eFee * $electric[$i], 2);
            $waterFee = empty($water[$i]) ? 0 : round($wFee * $water[$i], 2);

            $total += $electricFee + $waterFee;

            if(!empty($eList[$i])){
                $image->text("记电 {$eList[$i]}度  用电{$electric[$i]}度   {$electricFee}元.",
                    [
                        'fontSize' => 40,
                        'x'        => $printWidth,
                        'y'        => $eHeight,
                    ]
                );
            }

            if(!empty($wList[$i])){
                $image->text("记水 {$wList[$i]}度  用水{$water[$i]}度     {$waterFee}元.",
                    [
                        'fontSize' => 40,
                        'x'        => $printWidth,
                        'y'        => $wHeight,

                    ]
                );
            }

        }

        $image->text("合计 {$total}元",
            [
                'fontSize' => 48,
                'x'        => 1300,
                'y'        => 900,
            ]
        );
        ob_start ();
        $image->show();
        $image_data = ob_get_contents ();

        ob_end_clean ();

        echo base64_encode($image_data);

    }

    /**
     * 添加新的抄表记录
     */
    public function addFee(){
        $date = strtotime($this->p('fee_data').'-01 00:00:00');
        foreach ($_POST as $key => $value){
            if(strpos($key, 'electric_meters') === false && strpos($key, 'water_meters') === false){
                continue;
            }

            $this->db('unit_fee')->insert([
                'unit_id' => $this->p('id'),
                'unit_fee_date' => $date,
                'unit_fee_name' => $key,
                'unit_fee_value' => $value
            ]);

        }

        $this->success('完成添加', base64_decode($_POST['back_url']), '-1');

    }

    /**
     * 删除记录
     */
    public function delete(){
        $id = $this->isG('id', '请提交要删除的ID');
        $date = $this->isG('date', '请提交要删除的日期');
        $this->db('unit_fee')->where('unit_id = :unit_id AND unit_fee_date = :date ')->delete([
            'unit_id' => $id,
            'date' => $date
        ]);
        $this->success('删除完成', '', '-1');
    }

}